//server.cpp
//patrick braga-henebry
//mark pasquier

//std stuff
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

//net stuff
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>

#include <iostream>
#include <vector>
using namespace std;

//getopt
#include <getopt.h>

//#define PORT 2210
#define PORT 9998
#define BUFLEN 1024

int main(int argc, char * argv[])
{
	srand(time(NULL)); //now seed to RNG
	// network declarations
	int sockfd, packet_num, nbytes;
	socklen_t from_len = 0;
	char recved[BUFLEN];
	struct sockaddr_in my_addr;	// my address information
	struct sockaddr_in from; // connector's address information
	socklen_t addr_len;
	vector<int> message_buffer;
	vector<int>::iterator msg_iter;

	


	//getopt declarations
	int input = 0;
	int drop_pct;
	int dflag = 0;
	int drop_threshold = 10;
	//getopt
    while ((input = getopt (argc, argv, "p:d")) != -1)
        switch (input)
		{
		case 'd':
			cout << "d on!" << endl;
			dflag = 1;
			break;
		case 'p':
			drop_threshold = atoi(optarg);
			if (drop_threshold<0 || drop_threshold>100)
			{
		        fprintf (stderr, "drop_percent must be between 0 and 100 ", argv[0]);
				exit(1);
			}
			break;
		case '?':
			fprintf (stderr, "Usage: %s [-p drop_percent=10] [-d]", argv[0]);
			return 1;
		default:
			abort ();
		}

	//open socket
	if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
		perror("socket");
		exit(1);
	}
	
	//timeout of IP address thing
	int so_num = 1;
	if ( setsockopt( sockfd, SOL_SOCKET, SO_REUSEADDR, &so_num, sizeof(int)) == -1 )
	{ perror("setsockopt");	exit(1); }

	//netwrok setup stuff
	my_addr.sin_family = AF_INET;	
	my_addr.sin_port = htons(PORT);	 
	my_addr.sin_addr.s_addr = INADDR_ANY; 
	//memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);

	if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) == -1) {
		perror("bind");
		exit(1);
	}

	from_len = sizeof from;
	cout << "got herre" << endl;
	int flag = 0;
	while (1)
	{
		drop_pct = rand() % 100 ;
		cout << "dp: " << drop_pct << " go:" << (drop_threshold <= drop_pct) << endl;
		if (dflag) cout << "waiting for recv?" << endl;
		if ((nbytes = recvfrom(sockfd, recved, BUFLEN-1 , 0, (struct sockaddr *)&from, &from_len)) == -1)
		{
			perror("recvfrom");
			exit(1);
		}
		packet_num = atoi(recved);
		
		if (drop_threshold <= drop_pct)
		{
			
			for (msg_iter=message_buffer.begin() ; msg_iter != message_buffer.end() ; *msg_iter++ )
			{
				if (dflag) cout << "debug: " << *msg_iter << endl;
				if (*msg_iter == packet_num)
				{
					flag = 1;
					cout << "Msg " << packet_num << " already received" << endl;
					
				} 
			}
			if (flag)
			{
				flag = 0;
			}
			else{
				//if not executed, then it's a new packet. ACK THAT SHIZZ!
				cout << "ACK: " << packet_num << endl;
				message_buffer.push_back(packet_num); //store packet
				// send dat ACK
				nbytes = sendto(sockfd,recved,strlen(recved), 0,(struct sockaddr *)&from, from_len); 
				cout << "Sent ACK" << endl;			
			}
		}
		
		 close(sockfd);
	}
	
	
	
	return 1;
}


